#include <glib.h>
#include <stdlib.h>
#include <stdio.h>
#include <glib-object.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>

int main(int argc, char **argv) {
    g_type_init();
    DBusGConnection *conn = NULL;
    DBusGProxy *proxy = NULL;
    GError *error = NULL;
    conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
    if (conn == NULL) {
        fprintf(stderr, "cannot get system dbus");
        return 1;
    }
    proxy = dbus_g_proxy_new_for_name(conn, DBUS_SERVICE_DBUS,
            DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
    error = NULL;
    char **name_list;
    if (!dbus_g_proxy_call(proxy, "ListNames", &error, G_TYPE_INVALID, G_TYPE_STRV, &name_list,
                G_TYPE_INVALID)) {
        if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION) {
            fprintf(stderr, "caught remote method exception %s:%s", dbus_g_error_get_name(error),
                    error->message);
        } else {
            fprintf(stderr, "error %s\n", error->message);
        }
        g_error_free(error);
        return 1;
    }
    g_print("Names on the system bus:\n");
    char **name_list_ptr;
    for (name_list_ptr = name_list; name_list_ptr != NULL && *name_list_ptr != NULL; name_list_ptr++) {
        g_print("\t%s\n", *name_list_ptr);
    }
    g_strfreev(name_list);
    g_object_unref(proxy);

    return 0;
}
